package com.zhouyu.fileSync.FSclient.transmission.communicationBetweenClients.client;

import com.zhouyu.fileSync.FSclient.ClientAppContext;
import com.zhouyu.fileSync.FScommon.entities.FileRoute;

import java.util.concurrent.*;

import static com.zhouyu.fileSync.FSclient.transmission.communicationBetweenClients.client.CommunicationClientCenter.CommunicationClients;

public class CommunicationSyncJobSchedule implements Runnable {

    private final static BlockingQueue JOB_QUEUE = new ArrayBlockingQueue<Runnable>(ClientAppContext.CONFIGS.MAX_ACTIVE*4+ClientAppContext.CONFIGS.TARGETS.size());
    private final static ExecutorService executorService= new ThreadPoolExecutor(ClientAppContext.CONFIGS.MAX_ACTIVE,ClientAppContext.CONFIGS.MAX_ACTIVE*2,10, TimeUnit.MINUTES,JOB_QUEUE);

    public final static ConcurrentHashMap<String,Long> JOB_STATUS =new ConcurrentHashMap<>();

    public final static ConcurrentHashMap<FileRoute,Long> ONCE_JOB=new ConcurrentHashMap<>();

    @Override
    public void run() {
        while (true) {
            try {
                for (CommunicationClient client : CommunicationClients) {
                    if (!JOB_STATUS.containsKey(client.getCcId())) {
                        CommunicationSyncJob tsj = new CommunicationSyncJob(client);
                        executorService.submit(tsj);
                    }
                }
                //每隔一分钟进行一次任务的调度
                TimeUnit.MINUTES.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
